/**
 * Copyright ©2012-2018 HopeHaiyang Corporation, All Rights Reserved
 * http://www.wuhanhope.com
 */
package hope.platform.data.server.data.img;

import hope.platform.data.dao.HopeExceptionRepository;
import hope.platform.data.domain.HopeException;
import hope.platform.data.server.event.header.ExceptionListenerService;
import hope.platform.data.server.data.header.HopeExceptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * class info
 *
 * @author hujia
 *         Create at 2018/7/20 14:42
 */
@Service
public class HopeExceptionServiceImg implements HopeExceptionService {

    @Autowired
    private HopeExceptionRepository repository;

    @Autowired
    private ExceptionListenerService listenerService;

    @Override
    public List<HopeException> getAll() {
        return repository.findAll();
    }

    @Override
    public List<HopeException> getByGmtCreateBetween(Date beginTime, Date endTime) {
        return repository.findByGmtCreateBetween(beginTime,endTime);
    }

    @Override
    public List<HopeException> getByExceptionType(String exceptionType) {
        return repository.findByExceptionType(exceptionType);
    }

    @Override
    public List<HopeException> getByExceptionTypeAndSerialNumber(String exceptionType, String serialNumber) {
        return repository.findByExceptionTypeAndSerialNumber(exceptionType, serialNumber);
    }

    @Override
    public List<HopeException> getByExceptionTypeAndSerialNumberAndDataName(String exception, String serialNumber, String DataName) {
        return repository.findByExceptionTypeAndSerialNumberAndDataName(exception, serialNumber, DataName);
    }

    @Override
    public List<HopeException> getBySerialNumber(String serialNumber) {
        return repository.findBySerialNumber(serialNumber);
    }

    @Override
    public HopeException addHopeException(HopeException exception) {
        exception.setGmtCreate(new Date());
        exception.setGmtModified(new Date());
        listenerService.publish(exception);
        return repository.save(exception);
    }

    @Override
    public List<HopeException> getBySerialNumberAndDataNameAndGmtCreateBetween(
            String serialNumber, String dataName, Date beginTime, Date endTime) {
        return repository.findBySerialNumberAndDataNameAndGmtCreateBetween(serialNumber, dataName,
                beginTime, endTime);

    }

    @Override
    public List<HopeException> getBySerialNumberAndDataNameAndGmtCreateBetweenOrderByGmtCreateDesc(
            String serialNumber, String dataName, Date beginTime, Date endTime) {
        return repository.findBySerialNumberAndDataNameAndGmtCreateBetweenOrderByGmtCreateDesc(
                serialNumber, dataName, beginTime, endTime);
    }


    @Override
    public long countBySerialNumberAndDataNameAndGmtCreateBetween(
            String serialNumber, String dataName, Date beginTime, Date endTime) {
        return repository.countBySerialNumberAndDataNameAndGmtCreateBetween(serialNumber, dataName,
                beginTime, endTime);
    }
}
